/*
 * @Description: 网络请求框架
 * @Author: iamsmiling
 * @Date: 2021-05-25 11:49:08
 * @LastEditTime: 2021-06-16 07:45:39
 */
import 'package:credit_business_circle/R/R.dart';
import 'package:credit_business_circle/http_kit/exception/base_http_exception.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

enum TransitionAnimationType { none, switcher, fade }

/// 默认加载指示器
WidgetBuilder defaultLoadingBuilder = (BuildContext context) {
  return Center(
      child: CupertinoActivityIndicator(
    radius: 18,
  ));
};
WidgetBuilder defaultErrorBuilder = (BuildContext context) {
  return Text("出现异常");
};

WidgetBuilder defaultEmptyBuilder = (BuildContext context) {
  return Container(
    height: R.dimen.height,
    width: R.dimen.width,
    alignment: Alignment.center,
    child: Column(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        // Image.asset(R.image.empty),
        Container(
          margin: EdgeInsets.only(top: R.dimen.dp30),
          child: Text(
            "这里暂时是空的哦~",
            style: TextStyle(fontSize: R.dimen.sp13, color: R.color.ff666666),
          ),
        )
      ],
    ),
  );
};

///网络controller请求封装
abstract class BaseFutureLoadStateController<T> extends GetxController
    with StateMixin<T> {
  Future<T> loadData({Map? params});

  ///数据加载 状态统一处理
  Future fetchData() {
    return loadData().then((T value) {
      if (value is Iterable && value.isEmpty) {
        change(value, status: RxStatus.empty());
      } else {
        change(value, status: RxStatus.success());
      }
    }).catchError((err) {
      change(null,
          status: RxStatus.error(
              err is BaseHttpException ? err.message : err.toString()));
    });
  }

  @mustCallSuper
  @override
  void onInit() {
    fetchData();
    super.onInit();
  }
}

class FutureLoadStateBuilder<T extends BaseFutureLoadStateController>
    extends StatelessWidget {
  final T controller;
  final WidgetBuilder? loadingBuilder;
  final WidgetBuilder? errorBuilder;
  final WidgetBuilder? emptyBuilder;
  final WidgetBuilder? netoffBuilder;
  final GetControllerBuilder<T> builder;
  final TransitionAnimationType animationType;
  final Object? id;
  const FutureLoadStateBuilder(
      {Key? key,
      required this.controller,
      this.loadingBuilder,
      this.errorBuilder,
      this.emptyBuilder,
      this.netoffBuilder,
      required this.builder,
      this.animationType = TransitionAnimationType.none,
      this.id})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return controller.obx(
        (state) => Builder(
            builder: (context) => GetBuilder<T>(builder: builder, id: id)),
        onEmpty: Builder(builder: emptyBuilder ?? defaultEmptyBuilder),
        onError: (String? text) =>
            Builder(builder: errorBuilder ?? defaultErrorBuilder),
        onLoading: Builder(builder: loadingBuilder ?? defaultLoadingBuilder));
  }
}
